package xyz.xtt.equity.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.ApiModel;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 资产权益表
 * </p>
 *
 * @author author
 * @since 2024-12-30
 */
@Builder
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tbl_asset_entitlement")
@ApiModel(value = "TblAssetEntitlement对象", description = "资产权益表")
public class TblAssetEntitlement implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 主键
	 */
	@TableId(value = "id", type = IdType.AUTO)
	private Long id;

	/**
	 * 关联资产主键
	 */
	private Long assetId;

	/**
	 * 权益描述
	 */
	private String description;

	private String name;

	/**
	 * 获取条件
	 */
	private String requirement;

	/**
	 * 权益数量
	 */
	private BigDecimal amount;

	/**
	 * 销兑方
	 */
	private Long acceptUid;

	/**
	 * 销兑方名称
	 */
	private String acceptName;

	/**
	 * 销兑开始时间
	 */
	private Long acceptStartTime;

	/**
	 * 销兑结束时间
	 */
	private Long acceptEndTime;

	/**
	 * 销兑接口参数
	 */
	private String requestParam;

	/**
	 * 销兑文案描述
	 */
	private String acceptDesc;

	/**
	 * 销兑接口
	 */
	private String requestUrl;

	/**
	 * 三方业务id
	 */
	private String bizId;
	/**
	 * 业务类型
	 */
	private String bizType;
	/**
	 * 创建时间
	 */
	private Timestamp createTime;
	/**
	 * 业务来源
	 */
	private String businesSource;
	/**
	 * 兑换权益描述
	 */
	private String exchangeDesc;
	/**
	 * 兑换类型
	 */
	private int equityType;

	private boolean start() {
		return Objects.nonNull(acceptStartTime) && acceptStartTime > 0;
	}

	private boolean end() {
		return Objects.nonNull(acceptEndTime) && acceptEndTime > 0;
	}

	public boolean checkTime() {
		long curr = System.currentTimeMillis() / 1000;
		if (start() && end()) {
			return curr >= acceptStartTime && curr < acceptEndTime;
		} else if (start() && !end()) {
			return curr >= acceptStartTime;
		} else if (!start() && end()) {
			return curr < acceptEndTime;
		}
		return false;
	}

	public boolean checkParam() {
		return StringUtils.isBlank(requestParam) || StringUtils.isBlank(requestUrl) || StringUtils.isBlank(bizType);
	}

	public TblAssetEntitlementDeliver buildDeliver(Long appUserId, String responeText, Integer result) {
		return TblAssetEntitlementDeliver
				.builder()
				.id(IdUtil.getSnowflakeNextId())
				.assetId(assetId)
				.entitlementId(id)
				.appUserId(appUserId)
				.createTime(System.currentTimeMillis())
				.build();
	}

	public Boolean checkAmount() {
		return amount.compareTo(BigDecimal.ZERO) == 0;
	}

}
